package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/selectByPageServlet")
public class SelectByPageServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接收参数 当前页码 和 每页显示条数
        String currentPageStr = request.getParameter("currentPage"); // "4"
        String pageSizeStr = request.getParameter("pageSize"); // "5"

        int currentPage = Integer.parseInt(currentPageStr);
        int pageSize = Integer.parseInt(pageSizeStr);

        // 2.调用service查询，返回PageBean
        BrandService brandService = new BrandServiceImpl();
        PageBean pageBean = brandService.selectByPage(currentPage, pageSize);

        // 3.将PageBean转为JSON，写回客户端
        String json = JSON.toJSONString(pageBean);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(json);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
